﻿
Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_ShoppingCart_DetailMyShopping
    Inherits System.Web.UI.UserControl
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim _Creator As String = Session("UserName")

            Dim p As Order = (From q In data.Orders Where q.Id = hidID.Value And q.Creator = _Creator Select q).SingleOrDefault
            If Not p Is Nothing Then

                lblCustomername.Text = IIf(IsNothing(p.CustomerName) = True, "", p.CustomerName)
                lblCustomeraddress.Text = IIf(IsNothing(p.Address) = True, "", p.Address)
                lblCustomeremail.Text = IIf(IsNothing(p.Email) = True, "", p.Email)
                lblCustomerphone.Text = IIf(IsNothing(p.Phone) = True, "", p.Phone)
                lblOrderdate.Text = IIf(IsNothing(p.OrderDate) = True, "", CType(p.OrderDate, DateTime).ToString("dd/MM/yyyy HH:mm"))
                lblStatusid.Text = IIf(IsNothing(p.StatusId) = True, "", IIf(p.StatusId = 1, "Chờ xử lý", "Đã xử lý"))
                lblCustomernote.Text = IIf(IsNothing(p.Note) = True, "", p.Note)
                ShowShoppingCartList(p.CartId)

            End If
        End Using

    End Sub
    Private Sub ShowShoppingCartList(ByVal _CartId As String)
        Using Data As New VSEntities
            Dim p = (From q In Data.ShoppingCarts Where q.CartId = _CartId Select q).ToList
            Dim strKey_Name() As String = {"ProductID"}
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With

            Dim k As VSModel.CalculateSumPriceByCartID_Result = Data.CalculateSumPriceByCartID(Session.SessionID).FirstOrDefault
            If Not k Is Nothing AndAlso Not k.sumPrice Is Nothing Then
                lblGrossAmount.Text = FormatCurrency(k.sumPrice, ",", ".")
            End If

        End Using
    End Sub


#End Region
    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lbItem As Label = CType(e.Row.FindControl("lbItem"), Label)
            lbItem.Text = e.Row.RowIndex + 1

            Dim row As ShoppingCart = CType(e.Row.DataItem, VSModel.ShoppingCart)
            Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
            hplTitle.Text = row.Product.Title
            hplTitle.NavigateUrl = MainDomain & row.Product.QueryString & "-" & row.Product.ProductId.ToString & ".html"
            hplTitle.Target = "_blank"

            Dim lblProductCode As Label = CType(e.Row.FindControl("lblProductCode"), Label)
            lblProductCode.Text = row.Product.Code
            Dim lblNumber As Label = CType(e.Row.FindControl("lblNumber"), Label)
            lblNumber.Text = row.Quantity

            Dim lblGrossAmount As Label = CType(e.Row.FindControl("lblGrossAmount"), Label)
            lblGrossAmount.Text = CType(row.Quantity * row.Product.SellPrice, Double).ToString("#,###")
        End If
    End Sub
End Class
